package com.jltx.xq.appexitpro;

import android.app.Activity;
import android.app.Application;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author jltxseo
 * @date 2017/03/24.
 * @description:
 */

public class App extends Application {
    private static String TAG = "MyApp";

    private List<Activity> activityList;
    private static App instance;

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        init();
    }

    @Override
    public void onTerminate() {
        Log.d(TAG,"onTerminate");
        activityList.clear();
        super.onTerminate();
    }

    private void init(){
        activityList = Collections.synchronizedList(new ArrayList<Activity>()); //获取个线程安全的list
        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
                Log.d(TAG,activity.getLocalClassName() + " is created");
                activityList.add(activity);
            }

            @Override
            public void onActivityStarted(Activity activity) {

            }

            @Override
            public void onActivityResumed(Activity activity) {

            }

            @Override
            public void onActivityPaused(Activity activity) {

            }

            @Override
            public void onActivityStopped(Activity activity) {

            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

            }

            @Override
            public void onActivityDestroyed(Activity activity) {
                activityList.remove(activity);
                Log.d(TAG,activity.getLocalClassName() + " is destroyed");
            }
        });
    }

    public static App getApp() {
        return instance;
    }
    /**
     * 姿势一退出应用程序
     */
    public void exitApp() {
        Log.d(TAG,"remain activity count - " + activityList.size());
        for (int i= activityList.size() -1; i >= 0;i--){
            Activity activity = activityList.get(i);
            activity.overridePendingTransition(0,0);
            if(activity instanceof BaseAppCompatApp){
                ((BaseAppCompatApp) activity).onExitFinish();
            }
            activity.finish();
            Log.d(TAG,"finish activity - " + activity.getClass().getSimpleName());
        }
        activityList.clear();
//        Iterator<Activity> iterator = activityList.iterator();
//        while (iterator.hasNext()){
//            Activity activity = iterator.next();
//            activity.overridePendingTransition(0, 0);
//            if (activity instanceof BaseAppCompatApp) {
//                ((BaseAppCompatApp) activity).onExitFinish();
//            }
//            activity.finish();
//            iterator.remove();
//            Log.d(TAG, "finish activity - " + activity.getClass().getSimpleName());
//        }
        Thread mExitBrowserThread = new Thread() {
            public void run() {
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                android.os.Process.killProcess(android.os.Process.myPid());
            }
        };
        mExitBrowserThread.start();
    }

    /**
     * 姿势二民间
     * @param activity
     */
    public void exitApp1(Activity activity){
        Intent intent = new Intent();
        intent.setClass(activity, MainActivityNo.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        activity.overridePendingTransition(0,0);
        activity.startActivity(intent);
    }

}
